package com.stand.hardcode.controller;

import java.io.IOException;

import javax.servlet.*;
import javax.servlet.http.*;

import com.google.appengine.api.datastore.KeyFactory;
import com.stand.hardcode.entity.*;
import com.stand.hardcode.manager.*;
import com.stand.hardcode.util.*;

public class CreateVirtualShopServlet extends HttpServlet {
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		if (UserManager.IsLoggedIn(request)) {
			StandUser user = (StandUser)request.getSession().getAttribute(RequestAttributes.CURRENT_USER);
			String name = request.getParameter("name");
			
			VirtualShopManager shopManager = new VirtualShopManager();
			VirtualShop shop = shopManager.createShop(name, user.getEmail());
			
			String[] items = request.getParameterValues("items");
			
			if (items != null)
			{
				for (String item : items) {
					shop.addItem(KeyFactory.createKey(Item.class.getSimpleName(), item));
				}
			}
			
			request.setAttribute("message", "Virtual shop has been created successfully");
			getServletContext().getRequestDispatcher("/create_virtual_shop.jsp").forward(request, response);
		}
	}
}
